<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '执行单'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-form-model ref="ruleForm" :model="model" :rules="rules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="完成原因"
          prop="materialRemark"
          hasFeedback
        >
          <a-radio-group v-model="model.completeReason" :options="options">
          </a-radio-group>
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          prop="materialRemark"
          hasFeedback
        >
          <a-input
            placeholder="请输入备注"
            v-model.trim="model.completeRemark"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import CommodityModal from "@/components/CommodityModal.vue";
import { isNumber } from "@/unit/rule.js";
export default {
  props: {
    wsRelationId: {
      type: String,
    },
    worksheetId: {
      type: String,
    },
    workStageNum: {
      type: String,
    },
    userId: {
      type: String,
    },
    equipmentNumber: {
      type: String,
    },
  },
  components: { CommodityModal },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      model: {},
      goodsOption: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      options: [],
    };
  },
  mounted() {},
  computed: {
    rules() {
      return {
        materialCode: [{ required: true, message: "必填!" }],
        castCount: [
          { required: true },
          { validator: isNumber, trigger: "change" },
          { pattern: new RegExp(/^[1-9]d*$/), message: "投料数需要大于0" },
        ],
      };
    },
  },
  methods: {
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.getDictFindByCode("dict.execute.complete", "execute");
      this.model = Object.assign({}, record);
    },
    // 获取数据来源
    async getDictFindByCode(parentCode, Key) {
      let obj = {};
      let options = [];
      const res = await api.DictFindByCode({ parentCode });
      res.data.forEach((item) => {
        let { dictName, id } = item;
        obj = {
          label: dictName,
          value: id,
        };
        options.push(obj);
      });
      this.options = options;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    async submitVulFun() {
      this.confirmLoading = true;
      this.model.id = this.wsRelationId;
      this.model.worksheetId = this.worksheetId;
      let res;
      res = await api.ExecuteState(this.model);
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      console.log(this.equipmentNumber);
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.model = {};
      this.visible = false;
      this.$router.push({
        path: "/Punch",
        query: { equipmentNumber: this.equipmentNumber },
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },
  },
};
</script>

<style scoped lang="less">
::v-deep .ant-form-item-children-icon {
  display: none !important;
}

